<template>
  <div :class="className" :style="{ height: height, width: width }" />
</template>

<script>
import * as  echarts from "echarts";
require("echarts/theme/macarons"); // echarts theme
import resize from "../mixins/resize";

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: "chart",
    },
    width: {
      type: String,
      default: "100%",
    },
    height: {
      type: String,
      default: "260px",
    },
    chartdata: {
      type: Array,
    },
  },
  data() {
    return {
      chart: null,
    };
  },
  mounted() {
    this.$nextTick(() => {
      var that = this;
      var data = [];
      for (var i = 0; i < that.chartdata.length; i++) {
        data.push({
          name: that.chartdata[i].name,
          value: that.chartdata[i].count,
        });
      }
      this.initChart(data);
    });
  },
  beforeDestroy() {
    if (!this.chart) {
      return;
    }
    this.chart.dispose();
    this.chart = null;
  },
  methods: {
    initChart(data) {
      this.chart = echarts.init(this.$el, "macarons");

      this.chart.setOption({
        title: {
          text: "作业分类统计",
          left: "left",
          padding: [
            10, // 上
            10, // 右
            10, // 下
            10, // 左
          ],
          textStyle: { color: "#606266", fontSize: "14" },
        },
        tooltip: {
          trigger: "item",
        },
        legend: {
          orient: "vertical",
          top: "100px",
          left: "70%",
          textStyle: {  //数值样式
            color: '#606266',   //字体颜色
          }
        },
        series: [
          {
            name: "数据",
            type: "pie",
            radius: ["30%", "60%"],
            center: ["36%", "50%"],
            avoidLabelOverlap: false,
            label: {
              show: true,
            },
            emphasis: {
              label: {
                show: true,
                fontSize: "24",
                fontWeight: "bold",
              },
            },
            labelLine: {
              show: true,
            },
            data: data,
          },
        ],
      });
    },
  },
};
</script>
